package com.demo.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;

import com.appbase.custom.constant.RecordModeConstants;
import com.demo.helper.BatteryHelper;
import com.module.commonui.R;

public class SolarBatteryView extends LinearLayout {

    private ImageView mImageView;
    private TextView mTextView;
    private View mViewLine;
    private int mMode = 0;
    private int mBattery = 0;
    private int mRecordType = 0;
    private int mBatteryLow = 0;

    public void setRecordType(int recordType) {
        this.mRecordType = recordType;
        showUIInfo();
    }

    public SolarBatteryView(Context context) {
        super(context);
        initContentView(context);
    }

    public SolarBatteryView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SolarBatteryView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initContentView(context);
    }


    private void initContentView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.solar_battery, this, true);

        mImageView = view.findViewById(R.id.iv_battery);
        mTextView = view.findViewById(R.id.tv_text);
        mViewLine = view.findViewById(R.id.view_line);
    }


    public void setImageResource(@DrawableRes int resId){
        mImageView.setImageResource(resId);
    }



    public String getText(){
        return mTextView.getText().toString();
    }

    public void setText(String text){
        mTextView.setText(text);
    }


    public void  updateBatteryStatus(int battery, int mode, int batteryLow){
        if (battery == -1 && mode == -1) {
            setVisibility(GONE);
            return;
        }
        if (getVisibility() == GONE) {
            setVisibility(VISIBLE);
        }

        mMode = mode;
        mBattery = battery;
        mBatteryLow = batteryLow;

        showUIInfo();

    }

    private void showUIInfo() {
        BatteryHelper.setBattery(mImageView, mMode, mBattery, false);
        if (mRecordType == RecordModeConstants.RECORD_MODE_AUTO) {
            if (mBatteryLow == 0){
                mTextView.setText(R.string.txt_recording_full);
            }else{
                mTextView.setText(R.string.txt_event_recording);
            }
        } else if (mRecordType == RecordModeConstants.RECORD_MODE_ALARM) {
            mTextView.setText(R.string.txt_event_recording);
        } else if (mRecordType == RecordModeConstants.RECORD_MODE_FULLTIME) {
            mTextView.setText(R.string.txt_recording_full);
        }
    }
}
